package com.hongyuan.coach.ui.main.activity;

import android.os.Bundle;
import android.view.KeyEvent;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProviders;
import com.hongyuan.mvvmhabitx.base.AppManager;
import com.hongyuan.coach.R;
import com.hongyuan.coach.base.AppViewModelFactory;
import com.hongyuan.coach.base.CustomActivity;
import com.hongyuan.coach.data.Constants;
import com.hongyuan.coach.databinding.ActivityMainBinding;
import com.hongyuan.coach.ui.course.fragment.CourseFragment;
import com.hongyuan.coach.ui.home.fragment.HomeFragment;
import com.hongyuan.coach.ui.main.view_mode.MainViewModel;
import com.hongyuan.coach.ui.member.fragment.MembersFragment;
import com.hongyuan.coach.ui.person.fragment.PersonFragment;
import com.hongyuan.coach.utils.CustomDialog;
import com.hongyuan.coach.utils.PackageUtils;
import java.util.ArrayList;
import java.util.List;
import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;
import me.tatarka.bindingcollectionadapter2.BR;

public class MainActivity extends CustomActivity<ActivityMainBinding, MainViewModel> {

    private List<Fragment> mFragments;
    private  NavigationController navigationController;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MainViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(MainViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();

        mFragments = new ArrayList<>();
        mFragments.add(new HomeFragment());
        mFragments.add(new CourseFragment());
        mFragments.add(new MembersFragment());
        mFragments.add(new PersonFragment());
        commitAllowingStateLoss(0);

        initBottomTab();

        //检查是否需要更新
        viewModel.clearParams().setParams("app_version", PackageUtils.getVersionName(this))
                .setParams("app_type","1")
                .setParams("app_name","jl");
        viewModel.requestData(Constants.CHECK_APP_VERSION);
    }

    /*
    * 初始化底部tab
    * */
    private void initBottomTab(){
        navigationController = binding.tab.material()
                .addItem(R.mipmap.home_default,R.mipmap.home_select,"日程")
                .addItem(R.mipmap.course_default_icon_img,R.mipmap.course_select_icon_img,"课程")
                .addItem(R.mipmap.home_member_default_mark,R.mipmap.home_member_select_mark,"会员")
                .addItem(R.mipmap.person_default,R.mipmap.person_select,"我的")
                .setDefaultColor(getResources().getColor(R.color.theme_CCCCCC))
                .build();

        //底部按钮监听事件
        navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
                commitAllowingStateLoss(index);
            }

            @Override
            public void onRepeat(int index) {

            }
        });
    }

    /*
    * 显示某一个fragment
    * */
    private void commitAllowingStateLoss(int position){
        hideAllFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(String.valueOf(position));
        if(currentFragment != null){
            transaction.show(currentFragment);
        }else{
            currentFragment = mFragments.get(position);
            transaction.add(R.id.frameLayout, currentFragment, String.valueOf(position));
        }
        transaction.commitAllowingStateLoss();
    }

    /*
    * 隐藏所有fragment
    * */
    private void hideAllFragment(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        for (int i = 0 ; i < mFragments.size() ; i++){
            Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(String.valueOf(i));
            if(currentFragment != null){
                transaction.hide(currentFragment);
            }
        }
        transaction.commitAllowingStateLoss();
    }


    //安卓重写返回键事件
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            CustomDialog.promptDialog(this, "确定要退出程序？", "确定", "取消", false, v -> {
                if(v.getId() == R.id.isOk){
                    AppManager.getAppManager().finishAllActivity();
                }
            });
            return false;
        }
        return true;
    }
}
